package apibetaseries;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class APIHandler {
    public static String Key = "D4E90974F49D";
    public static String Login = "mystic";
    public static String Pwd = "1ebe44f95f8ea73542dc20a972a5430f";
    public static String Token = identification();
    
    public static void viewEpisode( TVShowNameParser toView ){
        if(toView.BSUrl.isEmpty())
            Utils.logger.warning("Unable to find " + toView.TVShow);
        else{
            Utils.logger.info("Sending to BetaSeries: " + toView.toString());
            
            // Add show to user
            Utils.getWebPage("http://api.betaseries.com/shows/add/" 
                    + toView.BSUrl + ".json"
                    + "?token=" + Token
                    + "&key=" + Key);
            
            // Mark episode as watched
            Utils.getWebPage("http://api.betaseries.com/members/watched/" 
                    + toView.BSUrl + ".json"
                    + "?season=" + toView.Season
                    + "&episode=" + toView.Episode
                    + "&token=" + Token
                    + "&key=" + Key);
            Utils.logger.info("Sended.");
        }
    }
    
    public static String identification(){
        String json = Utils.getWebPage( "http://api.betaseries.com/members/auth.json?login=" + Login + "&password=" + Pwd + "&key=" + Key);
        RegexMatch rm = Utils.matchRegex(json, "\\\"token\\\":\\\"([^\\\"]+)\\\"");
        if( rm.isFound ){
            Utils.logger.info("Logging as " + Login + " successful");
            return rm.matches[1];
        }
        else{
            Utils.logger.warning("Impossible to log as " + Login);
            return "";
        }
    }
    
    public static String[] searchShow( String show ){
        String json = Utils.getWebPage( "http://api.betaseries.com/shows/search.json?title=" + show + "&key=" + Key);
        RegexMatch rm = Utils.matchRegex(json, "\\\"url\\\":\\\"([^\\\"]+)\\\",\\\"title\\\":\\\"([^\\\"]+)\\\"");
        if( rm.isFound )
            return new String[] {rm.matches[1], rm.matches[2]};
        else{
            Utils.logger.info("Impossible to find the show '" + show + "'");
            return new String[] {null, null};
        }
    }
    
    public static List<String> getTimeline( String member, int size ){
        String json = Utils.getWebPage( "http://api.betaseries.com/timeline/member/" + member + ".json?number=" + size + "&key=" + Key);
        Pattern p = Pattern.compile(  "\\\"type\\\":\\\"([^\\\"]+)\\\","
                                    + "\\\"ref\\\":\\\"([^\\\"]+)\\\","
                                    + "\\\"login\\\":\\\"([^\\\"]+)\\\","
                                    + "\\\"data\\\":\\{([^\\}]+)\\}" 
                                   );
        Matcher m = p.matcher( json );
        
        List<String> result = new ArrayList<>();
        while( m.find() )
            switch (m.group(1)) {
                case "add_serie":
                    result.add("Add show: '" + m.group(2) + "'");
                    break;
                case "markas":
                    RegexMatch rmNumber = Utils.matchRegex( m.group(4), "\\\"number\\\":\\\"([^\\\"]+)\\\"" );
                    if( rmNumber.isFound )
                        result.add("Add episode: '" + m.group(2) + " - " + rmNumber.matches[1] + "'");
                    break;
            }
        return result;
    }
    
}
